--[[-------------------------------------------------------------------------
  Copyright (c) 2006-2007, Trond A Ekseth
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials provided
        with the distribution.
      * Neither the name of Trond A Ekseth nor the names of its
        contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------]]

local cmem, tmem
local frame = CreateFrame("Button", nil, UIParent, "SecureActionButtonTemplate")
local model = CreateFrame("Model", nil, frame)
local macro = "/stopcasting\n/use %d %s\n/use [button:2] 17; 16"

local GetID = function(item)
	if type(item) == "number" then return item
	elseif type(item) == "string" then
		local _, _, id = string.find(item, "item:(%d+):%d+:%d+:%d+")
		if id then return tonumber(id) end
	end
end
local S = function()
	return not TradeFrame:IsShown() and not BankFrame:IsShown() and not MerchantFrame:IsShown() and not InCombatLockdown()
end
local E = "3239 6951 20744 3775 20746 8926 20748 8927 20750 8928 3776 5237 23528 23529 2871 6530 10918 9186 21835 31535 10920 7965 10921 6529 10922 6532 6533 12404 20745 20747 22521 20749 2862 22522 28421 20844 18262 2863 3241 12643 3240 23559 22053 7964 6947 23122 23123 21927 22054 22055 25521 28420 6949 8985 2892 8984 6950 2893 "
local OnEnter = function()
	local s, b = this:GetID(), this:GetParent():GetID()
	local link = GetID(GetContainerItemLink(b, s))

	if(link and E:match(link.."%s") and S() and IsAltKeyDown()) then
		frame:Show()
		frame:SetAttribute("macrotext", macro:format(b, s))
		frame:ClearAllPoints()
		frame:SetAllPoints(this)
	end
end

frame:RegisterForClicks("LeftButtonUp", "RightButtonUp")
frame:SetFrameStrata"HIGH"
frame:SetScript("OnLeave", function(self) self:Hide() end)

frame:SetAttribute("*type*", "macro")

model:SetModel"Interface\\Buttons\\UI-AutoCastButton.mdx"
model:SetAllPoints(frame)
model:SetSequence(0)
model:SetSequenceTime(0, 0)
model:SetScale(1.3)

hooksecurefunc("ContainerFrameItemButton_OnEnter", OnEnter)

OnEnter = nil
